package com.alpaak.common.utils;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * @ClassName: DateUtil 
 * @Description:  日期工具类
 * @author: FengWenJu
 * @date: 2017年11月3日 下午8:22:15
 */
public class DateUtil {
	public static java.sql.Date converUtilTOSql(Date date) {
		return new java.sql.Date(date.getTime());
	}

	public static Date converSqlTOUtil(java.sql.Date date) {
		return date;
	}

	public static String converToString(Date dt, String format) {
		String returnStr = null;
		if (dt != null) {
			format = StringUtil.StringReplace("YYYY", "yyyy", format);
			format = StringUtil.StringReplace("DD", "dd", format);
			format = StringUtil.StringReplace("SS", "ss", format);
			format = StringUtil.StringReplace("hh24", "HH", format);
			format = StringUtil.StringReplace("HH24", "HH", format);
			format = StringUtil.StringReplace("MI", "mm", format);
			format = StringUtil.StringReplace("mi", "mm", format);
			if ((format == null) || (format.trim().equals(""))) {
				returnStr = DateFormat.getDateTimeInstance().format(dt);
			} else {
				SimpleDateFormat f = new SimpleDateFormat(format);
				returnStr = f.format(dt);
			}
		}
		return returnStr;
	}

	public static String converToString(String dt, String format) {
		java.sql.Date oneDay = converToDate(dt);
		return converToString(oneDay, format);
	}

	public static java.sql.Date converToDate(String s) {
		if ((s == null) || (s.equals(""))) {
			return null;
		}
		if (s.length() == 6) {
			s = s + "01";
		}

		String HH = "00";
		String mm = "00";
		String ss = "00";
		int len = s.length();
		String dd;
		String yyyy;
		String MM;
		if ((len == 8) && (CommonVerify.numberVerify(s))) {
			yyyy = s.substring(0, 4);
			MM = s.substring(4, 6);
			dd = s.substring(6, 8);
		} else {
			String val = "-";
			if (s.indexOf("/") >= 0) {
				val = "/";
			} else if (s.indexOf("年") > 0) {
				s = StringUtil.StringReplace("年", "-", s);
				s = StringUtil.StringReplace("月", "-", s);
				s = StringUtil.StringReplace("日", "", s);
				s = StringUtil.StringReplace("时", ":", s);
				s = StringUtil.StringReplace("分", ":", s);
				s = StringUtil.StringReplace("秒", "", s);
			}

			if (s.indexOf("-") < 0) {
				return null;
			}

			yyyy = s.substring(0, s.indexOf(val));
			MM = s.substring(s.indexOf(val) + 1, s.lastIndexOf(val));
			String temp = s.substring(s.lastIndexOf(val) + 1, s.length());
			if (temp.indexOf(" ") > 0) {
				dd = temp.substring(0, temp.indexOf(" "));
				temp = temp.substring(temp.indexOf(" ") + 1, temp.length());

				String[] a = StringUtil.getAsStringArray(temp.trim(), ":");
				if (a.length == 1) {
					HH = a[0];
				} else if (a.length == 2) {
					HH = a[0];
					mm = a[1];
				} else if (a.length >= 3) {
					HH = a[0];
					mm = a[1];
					ss = a[2];
					if (ss.indexOf(".") > 0) {
						ss = ss.substring(0, ss.indexOf("."));
					}
					if (ss.indexOf(" ") > 0)
						ss = ss.substring(0, ss.indexOf(" "));
				}
			} else {
				dd = temp.length() > 2 ? temp.substring(0, 2) : temp;
			}
		}
		return getDate(Integer.parseInt(yyyy), Integer.parseInt(MM),
				Integer.parseInt(dd), Integer.parseInt(HH),
				Integer.parseInt(mm), Integer.parseInt(ss));
	}

	public static Date converToDate(String dt, String format)
			throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.parse(dt);
	}

	public static java.sql.Date convertToDate(Timestamp ts) {
		if (ts == null) {
			return null;
		}
		return new java.sql.Date(ts.getTime());
	}

	public static Timestamp convertToTimestamp(Date dt) {
		return dt == null ? null : new Timestamp(dt.getTime());
	}

	public static java.sql.Date getDate(int yyyy, int MM, int dd) {
		return getDate(yyyy, MM, dd, 0, 0, 0);
	}

	public static java.sql.Date getDate(int yyyy, int MM, int dd, int HH,
			int mm, int ss) {
		if (!CommonVerify.dateVerify(yyyy, MM, dd))
			throw new IllegalArgumentException("不是有效的时间");
		Calendar oneCalendar = Calendar.getInstance();
		oneCalendar.clear();
		oneCalendar.set(yyyy, MM - 1, dd, HH, mm, ss);
		return new java.sql.Date(oneCalendar.getTime().getTime());
	}

	public static String getDateToOracleFormat(Date date) {
		return converToString(date, "YYYY-MM-DD HH24:MI:SS");
	}

	public static int getYear() {
		Calendar calendar = Calendar.getInstance();
		int yyyy = calendar.get(1);
		return yyyy;
	}

	public static int getYear(Date dt) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(dt);
		int yyyy = calendar.get(1);
		return yyyy;
	}

	public static int getYear(String dateStr) {
		java.sql.Date oneDay = converToDate(dateStr);
		Calendar ca = Calendar.getInstance();
		ca.clear();
		ca.setTime(oneDay);
		return ca.get(1);
	}

	public static java.sql.Date getStepYear(Date date, int n) {
		Calendar calo = Calendar.getInstance();
		calo.setTime(date);
		calo.add(1, n);
		return converUtilTOSql(calo.getTime());
	}

	public static java.sql.Date getStepMonth(Date date, int n) {
		Calendar calo = Calendar.getInstance();
		calo.setTime(date);
		calo.add(2, n);
		return converUtilTOSql(calo.getTime());
	}

	public static String getStepMonth(String dateStr, int step) {
		if (dateStr == null) {
			return null;
		}
		if ((dateStr != null) && (dateStr.length() == 6))
			dateStr = dateStr + "01";
		java.sql.Date oneDay = converToDate(dateStr);
		Calendar ca = Calendar.getInstance();
		ca.clear();
		ca.setTime(oneDay);

		ca.set(2, ca.get(2) + step);

		int yyyy = ca.get(1);
		int MM = ca.get(2) + 1;
		String month = MM + "";

		if (MM < 10)
			month = "0" + MM;
		return yyyy + month;
	}

	public static java.sql.Date getStepDay(Date date, int step) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(6, step);
		return new java.sql.Date(calendar.getTime().getTime());
	}

	public static String getStepDay(Date date, int step, String type) {
		return converToString(getStepDay(date, step), type);
	}

	public static int getIntervalDay(Date startDate,
			Date endDate) {
		if ((startDate == null) || (endDate == null)) {
			return 0;
		}
		long startdate = startDate.getTime();
		long enddate = endDate.getTime();
		long interval = enddate - startdate;
		double day = interval / 86400000L;
		int intervalday = (int) day;
		if (day > intervalday)
			intervalday++;
		return intervalday;
	}

	public static int getIntervalSecond(Date startDate,
			Date endDate) {
		if ((startDate == null) || (endDate == null)) {
			return 0;
		}
		long startdate = startDate.getTime();
		long enddate = endDate.getTime();
		long interval = enddate - startdate;
		double second = interval / 1000L;
		int intervalSecond = (int) second;
		if (second > intervalSecond)
			intervalSecond++;
		return intervalSecond;
	}

	public static int getIntervalMonth(String s, String s1) {
		try {
			String startDate = converToString(s, "yyyyMM");
			String endDate = converToString(s1, "yyyyMM");
			int i = Integer.parseInt(startDate.substring(0, 4)) * 12
					+ Integer.parseInt(startDate.substring(4, 6));
			int j = Integer.parseInt(endDate.substring(0, 4)) * 12
					+ Integer.parseInt(endDate.substring(4, 6));
			int intervalMonth = j - i;
			return intervalMonth;
		} catch (Exception e) {
		}
		return 0;
	}

	public static int getIntervalMonth(Date startDate,
			Date endDate) {
		SimpleDateFormat f = new SimpleDateFormat("yyyyMM");
		String str = f.format(startDate);
		String end = f.format(endDate);
		return getIntervalMonth(str, end);
	}

	public static boolean IfLeap(int y) {
		if (y % 400 == 0)
			return true;
		if (y % 100 == 0)
			return false;
		return y % 4 == 0;
	}

	public static String descreaseYearMonth(String dt) {
		String s = converToString(dt, "yyyyMMdd");
		int i = new Integer(s.substring(0, 4)).intValue();
		int j = new Integer(s.substring(4, 6)).intValue();
		j--;
		if (j >= 10)
			return s.substring(0, 4) + new Integer(j).toString();
		if ((j > 0) && (j < 10)) {
			return s.substring(0, 4) + "0" + new Integer(j).toString();
		}
		return new Integer(i - 1).toString() + new Integer(j + 12).toString();
	}

	public static int getAge(Date birthday, Date endDate) {
		int month = getIntervalMonth(birthday, endDate);
		if (month <= 0) {
			return 0;
		}
		return month / 12;
	}

	public static int getAge(String id) throws Exception {
		int i = -1;
		int j = id.length();
		String s1 = "";
		if (j == 15) {
			s1 = id.substring(6, 8);
			s1 = "19" + s1;
		} else if (j == 18) {
			s1 = id.substring(6, 10);
		} else {
			throw new Exception("错误的身份证号");
		}
		int k = Calendar.getInstance().get(1);
		i = k - new Integer(s1).intValue();
		return i;
	}

	public static java.sql.Date getBirtday(int age) {
		Calendar calendar = Calendar.getInstance(Locale.CHINESE);
		calendar.set(calendar.get(1) - age, calendar.get(2), calendar.get(5));
		return converUtilTOSql(new Date(calendar.getTimeInMillis()));
	}

	public static boolean yearMonthGreater(String dt1, String dt2) {
		return yearMonthGreater(converToDate(dt1), converToDate(dt2));
	}

	public static boolean yearMonthGreater(Date dt1,
			Date dt2) {
		String s = converToString(dt1, "yyyyMM");
		String s1 = converToString(dt2, "yyyyMM");
		String s2 = s.substring(0, 4);
		String s3 = s1.substring(0, 4);
		String s4 = s.substring(4, 6);
		String s5 = s1.substring(4, 6);
		if (Integer.parseInt(s2) > Integer.parseInt(s3))
			return true;
		if (Integer.parseInt(s2) == Integer.parseInt(s3)) {
			return Integer.parseInt(s4) > Integer.parseInt(s5);
		}
		return false;
	}

	public static String month2YearMonth(String s) {
		String s1 = "";
		if ((s == null) || ("0".equals(s)) || ("".equals(s.trim())))
			return "0月";
		int i = Integer.parseInt(s);
		int j = i / 12;
		int k = i % 12;
		if (j > 0)
			s1 = j + "年";
		if (k > 0)
			s1 = s1 + k + "个月";
		return s1;
	}

	/**
	 *与现在相隔的秒数，获得时间
	 * @param millisecond
	 * @return
	 */
	public static Date getDateFromIntervalMillisecond(String millisecond) {
		Date date = new Date();
		date.setTime(Long.valueOf(millisecond)+Calendar.getInstance().getTimeInMillis());
		return date;
	}
	public static void main(String[] args) {
		System.out.println(DateUtil.getDateFromIntervalMillisecond("3600000"));
	}
}